package shopping;
import java.util.Scanner;

//购物系统类
class ShoppingSystem {
    //分类别存储商品
    private String[] drinks ;
    private String[] fruits ;
    private String[] vegetables;
    private String[] meats ;
    private String[] others;

    //存储对应商品单价
    private double[] priceOfDrinks;
    private double[] priceOfFruits ;
    private double[] priceOfVegetables;
    private double[] priceOfMeats ;
    private double[] priceOfOthers = new double[20];

    //存储商品分类信息
    String[] variety;

    //购物车方法录入商品所用变量
    private String[] cartGoods = new String[20];
    private int[] cartAmount = new int[20];
    private int k = 0;


    //用户自行添加商品方法
    public void addGoods() {
        //定义循环变量
        int dri = 3;
        int fru = 3;
        int veg = 3;
        int mea = 3;
        int oth=0;
        int v = 0;
        //输出开始添加商品提示信息
        System.out.println("现在您可以自行添加商品并选购，系统将在最后统一计算价格");
        System.out.println("您可自行添加一个商品种类，或者在已有的商品分类里添加商品");
        System.out.println();
        System.out.println("目前我们有如下商品种类");
        for (int i = 0; i < this.variety.length; i++) {
            if (this.variety[i] != null) {
                System.out.print(this.variety[i] + " ");
            }
        }
        System.out.println();
        //输入商品类别
        System.out.println("请输入添加或选择的商品类别");
        Scanner addKind = new Scanner(System.in);
        String addKinds = addKind.nextLine();
        while (true) {
            //判断用户选择了已有类别还是进行添加操作
            if (addKinds.equals(this.variety[v]) && v < 4) {
                System.out.println("您已成功选择" + this.variety[v] + "分类，" +
                        "现在您可以添加具体商品。已有类别的商品添加数量不超过17,"
                        + "请输入“结束”以停止添加。");
                break;
            } else {
                v++;
            }
            if (v == 4) {
                this.variety[4] = addKinds;
                System.out.print("您已成功添加" + this.variety[4] + "分类，"
                        + "现在您可以添加具体商品，新建类别的商品添加数量不超过20，"
                        + "请输入“结束”以停止添加");
                System.out.println();
                break;
            }
        }
        //开始输入具体商品信息
        while (true) {
            Scanner addGood = new Scanner(System.in);
            System.out.println("请输入商品名称: ");
            String addGoods = addGood.nextLine();
            //判断是否结束
            if (addGoods.equals("结束")) {
                break;
            }
            System.out.println("请输入该商品单价: ");
            double addPrices = addGood.nextDouble();
            //正在添加“饮品”类别
            if (addKinds.equals("饮品")) {
                this.drinks[dri] = addGoods;
                this.priceOfDrinks[dri] = addPrices;
                System.out.println("dri="+dri);
                dri++;
                //判断添加数量是否超出
                if (dri >= 20) {
                    System.out.println("您已添加17件商品，系统已自动退出");
                    break;
                }
            }
            //正在添加“水果类别”
            else if (addKinds.equals("水果")) {
                this.fruits[fru] = addGoods;
                this.priceOfFruits[fru] = addPrices;
                fru++;
                //判断添加数量是否超出
                if (fru >= 20) {
                    System.out.println("您已添加17件商品，系统已自动退出");
                    break;
                }
            }
            //正在添加“蔬菜”类别
            else if (addKinds.equals("蔬菜")) {
                this.vegetables[veg] = addGoods;
                this.priceOfVegetables[veg] = addPrices;
                veg++;
                //判断添加数量是否超出
                if (veg >= 20) {
                    System.out.println("您已添加17件商品，系统已自动退出");
                    break;
                }
            }
            //正在添加“肉类”类别
            else if (addKinds.equals("肉类")) {
                this.meats[mea] = addGoods;
                this.priceOfMeats[mea] = addPrices;
                mea++;
                //判断添加数量是否超出
                if (mea >= 20) {
                    System.out.println("您已添加17件商品，系统已自动退出");
                    break;
                }
            }
            //正在添加用户自行输入的类别
            else {
                this.others[oth]=addGoods;
                this.priceOfOthers[oth]=addPrices;
                oth++;
                //判断添加数量是否超出
                if (oth >= 20) {
                    System.out.println("您已添加20件商品，系统已自动退出");
                    break;
                }
            }
        }
    }

    //类别选择方法
    public int chooseKinds(){
        //定义返回值变量
        int retKinds =0;

        //再次展示已有类别
        System.out.println("目前我们有如下商品种类");
        for(int i=0;i<this.variety.length;i++){
            if(this.variety[i]!=null) {
                System.out.print(this.variety[i] + " ");
            }
        }
        System.out.println();

        //提示选择商品类别
        System.out.println("请选择一个类别以查看详细商品信息");

        //选择查看的类别
        Scanner check=new  Scanner(System.in);
        System.out.print("请输入: ");
        String checkGoods = check.nextLine();

        //查看“饮品”类别
        if (checkGoods.equals("饮品")) {
            for(int i=0;i<this.drinks.length;i++){
                if(this.drinks[i]!=null) {
                    System.out.print(this.drinks[i] + " ");
                    System.out.println(this.priceOfDrinks[i]+" ");
                }
            }
            System.out.println();
            retKinds=11;
        }
        //查看“水果”类别
        else if(checkGoods.equals("水果")){
            for(int i=0;i<this.fruits.length;i++){
                if(this.fruits[i]!=null) {
                    System.out.print(this.fruits[i] + " ");
                    System.out.println(this.priceOfFruits[i]+" ");
                }
            }
            System.out.println();
            retKinds=22;
        }
        //查看“蔬菜”类别
        else if(checkGoods.equals("蔬菜")){
            for(int i=0;i<this.vegetables.length;i++){
                if(this.vegetables[i]!=null) {
                    System.out.print(this.vegetables[i] + " ");
                    System.out.println(this.priceOfVegetables[i]+" ");
                }
            }
            System.out.println();
            retKinds=33;
        }
        //查看“肉类”类别
        else if(checkGoods.equals("肉类")){
            for(int i=0;i<this.meats.length;i++){
                if(this.meats[i]!=null) {
                    System.out.print(this.meats[i] + " ");
                    System.out.println(this.priceOfMeats[i]+" ");
                }
                System.out.println();
            }
            retKinds=44;
        }
        //查看自行添加的类别
        else if(this.variety[4]!=null) {
            if (checkGoods.equals(this.variety[4])) {
                for (int i = 0; i < this.others.length; i++) {
                    if (this.others[i] != null) {
                        System.out.print(this.others[i]);
                        System.out.println(this.priceOfOthers[i]+" ");
                    }
                }
                System.out.println();
                retKinds=55;
            }
        }
        //用户输入错误
        else{
            retKinds=1;
        }
        //返回用户输入情况
        return retKinds;
    }



    //添加购物车方法
    public void addCart(int trail) {
        //定义购物车方法所用变量
        String[] cart = new String[20];

        //获取目前用户正在选购的商品类别
        switch (trail) {
            //“饮品”类别
            case 11 -> {
                cart = this.drinks;
            }
            //“水果”类别
            case 22 -> {
                cart = this.fruits;
            }
            //“蔬菜”类别
            case 33 -> {
                cart = this.vegetables;
            }
            //“肉类”类别
            case 44 -> {
                cart = this.meats;
            }
            //用户自行添加类别
            case 55 -> {
                cart = this.others;
            }
        }

        //提示开始添加商品到购物车
        System.out.println("输入商品名称和购买数量以将其添加到购物车,输入“结束”以停止添加");

        //可多次输入
        while (true) {
            //定义判断变量
            int judge = 0;
            //提示输入
            System.out.println("请输入商品名称: ");
            Scanner se = new Scanner(System.in);
            String chooseGoods = se.nextLine();
            //判断是否添加结束
            if (chooseGoods.equals("结束")) {
                break;
            }
            System.out.println("请输入购买数量: ");
            int amount = se.nextInt();
            //录入商品到购物车变量
            for (int j = 0; j < cart.length; j++) {
                if(cart[j]!=null) {
                    if (cart[j].equals(chooseGoods)) {
                        System.out.println("k=" + k);
                        this.cartGoods[k] = chooseGoods;
                        this.cartAmount[k] = amount;
                        k++;
                        judge = 1;
                    }
                }
            }
            //用户输入错误，提示重新输入
            if (judge == 0) {
                System.out.println("该商品不存在，请重新输入！");
            }
            //添加商品数量超额，自动退出
            if (k == 20) {
                System.out.println("购物车最多添加20件商品，已自动退出！");
                break;
            }
        }
    }

    //查看购物车方法
    public void checkCart() {
        //提示开始核对选购信息
        System.out.println("请查看并核对您的购物车信息");
        //循环输出所选商品信息
        for (int i = 0; i < cartGoods.length; i++) {
            if (cartAmount[i] != 0) {
                System.out.print(cartGoods[i] + " ");
                System.out.println(cartAmount[i]);
            }
        }
        System.out.println();
    }

    //删除购物车商品方法
    public void removeCart() {
        //定义判断变量
        int ifClean = 0;
        int ifEquals = 0;

        //可多次输入
        while (true) {
            System.out.println("请输入想要删除的商品名称");
            Scanner sg = new Scanner(System.in);
            System.out.print("请输入: ");
            String remove = sg.nextLine();
            //判断是否输入结束
            if (remove.equals("结束")) {
                System.out.println("删除成功，您将再次进入购物车界面");
                break;
            }
            //判断购物车是否仅剩一件商品
            if (ifClean == k - 1) {
                System.out.println("您的购物车里仅剩一件商品，已自动退出！");
                break;
            }
            //将选择删除的商品信息替换为空
            for (int rem = 0; rem < k; rem++) {
                if (cartGoods[rem].equals(remove)) {
                    System.out.println("rem=" + rem);
                    cartGoods[rem] = "";
                    cartAmount[rem] = 0;
                    ifEquals++;
                }
            }
            //用户输入错误，提示重新输入
            if (ifEquals < ifClean + 1) {
                System.out.println("该商品不存在，请重新输入！");
            }
            ifClean = ifEquals;
        }
    }

    //计算总价方法
    public void calculate() {
        //定义计算变量
        double total = 0;
        //开始计算
        for (int i = 0; i < this.cartGoods.length; i++) {
            if (this.cartGoods[i] != null) {
                for (int j = 0; j < this.drinks.length; j++) {
                    if (this.cartGoods[i].equals(this.drinks[j])) {
                        total += this.cartAmount[i] * this.priceOfDrinks[j];
                    }
                }

                for (int j = 0; j < this.fruits.length; j++) {
                    if (this.cartGoods[i].equals(this.fruits[j])) {
                        total += this.cartAmount[i] * this.priceOfFruits[j];
                    }
                }
                for (int j = 0; j < this.vegetables.length; j++) {
                    if (this.cartGoods[i].equals(this.vegetables[j])) {
                        total += this.cartAmount[i] * this.priceOfVegetables[j];
                    }
                }
                for (int j = 0; j < this.meats.length; j++) {
                    if (this.cartGoods[i].equals(this.meats[j])) {
                        total += this.cartAmount[i] * this.priceOfMeats[j];
                    }
                }
                for (int j = 0; j < this.others.length; j++) {
                    if (this.cartGoods[i].equals(this.others[j])) {
                        total += this.cartAmount[i] * this.priceOfOthers[j];
                    }
                }
            }
        }

        //输出结果
        System.out.printf("总价为 %4.2f 元\n", total);
    }


    //预设商品信息方法
    //预设商品类别
    public void setVariety(String[] kinds) {
        this.variety = new String[5];
        for (int i = 0; i < 4; i++) {
            this.variety[i] = kinds[i];
        }
    }
    //预设“饮品”类商品
    public void setDrinks(String[] drinks,double[] priceOfDrinks) {
        this.drinks = new String[20];
        this.priceOfDrinks = new double[20];
        for (int i = 0; i < 3; i++) {
            this.drinks[i] = drinks[i];
            this.priceOfDrinks[i] = priceOfDrinks[i];
        }
    }
    //预设“水果”类商品
    public void setFruits(String[] fruits,double[] priceOfFruits) {
        this.fruits = new String[20];
        this.priceOfFruits = new double[20];
        for (int i = 0; i < 3; i++) {
            this.fruits[i] = fruits[i];
            this.priceOfFruits[i] = priceOfFruits[i];
        }
    }
    //预设“蔬菜”类商品
    public void setVegetables(String[] vegetables,double[] priceOfVegetables) {
        this.vegetables = new String[20];
        this.priceOfVegetables = new double[20];
        for (int i = 0; i < 3; i++) {
            this.vegetables[i] = vegetables[i];
            this.priceOfVegetables[i] = priceOfVegetables[i];
        }
    }
    //预设“肉类”类商品
    public void setMeats(String[] meats,double[] priceOfMeats) {
        this.meats = new String[20];
        this.priceOfMeats = new double[20];
        for (int i = 0; i < 3; i++) {
            this.meats[i] = meats[i];
            this.priceOfMeats[i] = priceOfMeats[i];
        }
    }
    public void setOthers() {
        this.others = new String[20];
    }



}
